import jwt_decode from 'jwt-decode';
import Taro from '@tarojs/taro'
import { WXRequest } from './../utils/WXRequest';
import { SET_USER, SET_USERID } from './../constants/user';
import { URL_LOGIN_WXMP, URL_LOGIN_REFERSHTOKEN, URL_USER, URL_SUBMITINFO } from './../constants/SystemConstants';
import { LOGIN_PENDING, LOGIN_RESOLVE, LOGIN_REJECT, LOGIN_REFERSH_TOKEN } from './../constants/login';
import HttpClients from '../utils/HttpClients';
// 异步的action

export const loginWXMP = () => async dispatch => {
  // dispatch({
  //   type: LOGIN_PENDING
  // })
  try {
    if (process.env.TARO_ENV !== 'alipay' && process.env.TARO_ENV !== 'weapp') {
      // const resp = await HttpClients.post(URL_LOGIN_WXMP, data)
      // dispatch({
      //   type: LOGIN_RESOLVE,
      //   result: resp
      // })
    } else {
      Taro.getUserInfo({
        success: (userInfo => {
          Taro.login({
            success: (loginData => {
              let data = {
                ...userInfo,
                code: loginData.code,
                appId: 'wxa464dec47728aee5',
                rawData: {
                  ...userInfo
                }
              }
              Taro.request({
                url: URL_LOGIN_WXMP,
                method: 'POST',
                data: data,
                success: (res => {
                  if (res.statusCode >= 200 && res.statusCode < 300) {
                    dispatch({
                      type: LOGIN_RESOLVE,
                      token: res.data
                    })
                    // token同步
                    setTimeout(() => {
                      dispatch({
                        type: SET_USER,
                        user: data
                      })
                      const jwtData = jwt_decode(res.data.access_token)
                      dispatch({
                        type: SET_USERID,
                        userId: jwtData.id
                      })
                      WXRequest(URL_SUBMITINFO, 'POST', {...userInfo.userInfo}).then(res => {
                        
                      })
                    }, 500)
                    return
                  }
                  dispatch({
                    type: LOGIN_REJECT,
                    result: '网络错误'
                  })
                }),
                fail: (e => {
                  dispatch({
                    type: LOGIN_REJECT,
                    result: e.errMsg
                  })
                })
              })
            }),
            fail: (loginFail => {
              console.error(loginFail.errMsg)
            })
          })
        }),
        fail: (e => {
          Taro.navigateTo({
            url: '/pages/auth/MPLogin'
          })
        })
      })
      
    }
    
  } catch (e) {
    console.error(e)
    dispatch({
      type: LOGIN_REJECT,
      result: e.message
    })
  }
}

export const refershToken = (token) => async dispatch => {
  try {
    if (process.env.TARO_ENV !== 'alipay' && process.env.TARO_ENV !== 'weapp') {
      // 非小程序环境
    } else {
      WXRequest(URL_LOGIN_REFERSHTOKEN, 'POST', {
        refresh_token: token
      }).then(res => {
        dispatch({
          type: LOGIN_REFERSH_TOKEN,
          token: res.data
        })
      }).catch(e => {

      })
    }
  } catch (e) {
    console.error(e)
  }
}







// export function loginWXMP () {
//   return dispatch => {
//     setTimeout(() => {
//       dispatch(add())
//     }, 2000)
//   }
// }
